package com.kbbywyclient.ui.activity.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.kbbywyclient.app.App;
import com.kbbywyclient.data.entity.User;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.util.pre.PreUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by FHW
 * on 2017/3/21
 */

public abstract class BaseActivity extends AppCompatActivity {

    private CompositeSubscription mCompositeSubscription;
    @Inject
    ProgressDialog progressDialog;//进度框

    private Tracker mTracker;

    protected abstract int provideContentViewId();

    protected abstract void initView();

    public abstract void initData();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(provideContentViewId());
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //这个属性4.4算是全透明（有的机子是过渡形式的透明），5.0就是半透明了 我的模拟器、真机都是半透明，
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
        }

        PushAgent.getInstance(this).onAppStart();//此方法与统计分析sdk中统计日活的方法无关！请务必调用此方法！

        initView();//初始控件
        initAppComponent(App.getInstance().getAppComponent());//注入avtivity

        progressDialog = new ProgressDialog(this);
        initData();
    }

    public void setScreenName(String s) {
        mTracker = App.getInstance().getDefaultTracker();
        mTracker.setScreenName(s);
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }

    public void setScreenAction(String category, String action) {
        mTracker = App.getInstance().getDefaultTracker();
//        mTracker.setScreenName(s);
        mTracker.send(new HitBuilders.EventBuilder()
                .setCategory(category)
                .setAction(action)
                .build());
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * add subscription, unsubscribe it when activity ondestory
     *
     * @param subscription
     */
    public void addSubscription(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(subscription);
    }

    public abstract void initAppComponent(AppComponent appComponent);

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }


    public void showProgress() {
        showProgress("", "");
    }

    public void showProgress(String content) {
        showProgress("", content);
    }

    public void showProgress(int contentRes) {
        showProgress("", getString(contentRes));
    }

    /**
     * 显示进度框
     *
     * @param title
     * @param content
     */
    public void showProgress(String title, String content) {
        if (progressDialog != null)
            if (progressDialog.isShowing())
                progressDialog.dismiss();
        progressDialog.setTitle(title);
        progressDialog.setMessage(content);
        progressDialog.show();
    }

    /**
     * 隐藏进度框
     */
    public void dismissProgress() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    /**
     * 获取用户信息
     *
     * @param context
     * @return
     */
    public User getUser(Context context) {
        return PreUtils.getUser(context);
    }

}
